﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace ReflectionStudio.LocalizationLibrary
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Windows;
    
    /// <summary>
    /// Represents a localization model.
    /// </summary>
    public class LocalizationModel : ILocalizationModel
    {
        //Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        //Constructors

        /// <summary>
        /// Default constructor.
        /// </summary>
        public LocalizationModel()
        {
            LocalizationManager.RegisterLocalizationModel(this);
            this.CurrentLanguage = LocalizationManager.CurrentLanguage;
            this.AvailableLanguages = new List<Language>(Languages.AvailableLanguages);
        }
        public IEnumerable<Language> AvailableLanguages { get; set; }
        private Language _currentLanguage;
        public Language CurrentLanguage
        {
            get
            {
                return this._currentLanguage;
            }
            set
            {
                if (this._currentLanguage != value)
                {
                    this._currentLanguage = value;
                    if ((this._currentLanguage != null) &&
                        (!LocalizationManager.CurrentLanguage.Code.Equals(this._currentLanguage.Code)))
                    {
                        LocalizationManager.CurrentLanguage = this.CurrentLanguage;
                    }
                    this.OnPropertyChange("CurrentLanguage");
                }
            }
        }
        //Methods

        /// <summary>
        /// Publishes the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName"></param>
        protected internal void OnPropertyChange(String propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}